package com.app.shop.activitys;

import android.content.ContentValues;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.app.shop.R;
import com.app.shop.adapters.GoodsListAdapter;
import com.app.shop.beans.GoodsBean;
import com.app.shop.beans.OrderBean;
import com.app.library.base.BaseActivity;
import com.app.library.beans.EventMessage;
import com.app.library.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

public class  OrderDetailActivity extends BaseActivity {


    private TextView tvTitle;
    private  RecyclerView recy;
    private  TextView tvPrice;

    private List<GoodsBean> list = new ArrayList<>();
    private GoodsListAdapter adapter;

    Bundle bundle;
    OrderBean orderBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        tvTitle= (TextView) findViewById(R.id.tv_title);
        recy= (RecyclerView) findViewById(R.id.recy);
        tvPrice= (TextView) findViewById(R.id.tv_price);
       ImageView imgvreturn= (ImageView) findViewById(R.id.imgv_return);
        imgvreturn.setOnClickListener(this::onViewClicked);
        tvTitle.setText("订单详情");
        initData();
    }
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.imgv_return:
                onBackPressed();
                break;
        }
    }
    private void initData() {
        bundle = getIntent().getExtras();
        orderBean = (OrderBean) bundle.getSerializable("bean");
        tvPrice.setText("总价：" + orderBean.getPrice());
        String[] str_goodsid = orderBean.getGoods_ids().split(",");
        List<GoodsBean> list_goods = DataSupport.findAll(GoodsBean.class);
        list.clear();
        for (int i = 0; i < list_goods.size(); i++) {
            for (int j = 0; j < str_goodsid.length; j++) {
                if (list_goods.get(i).getGoods_id().equals(str_goodsid[j])) {
                    list.add(list_goods.get(i));
                }
            }
        }

        adapter = new GoodsListAdapter(R.layout.item_goods_list, list, 1);
        adapter.setEmptyView(LayoutInflater.from(this).inflate(R.layout.view_list_empty, null));
        recy.setLayoutManager(new LinearLayoutManager(this));
        recy.setAdapter(adapter);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("bean", list.get(position));
                showActivity(OrderDetailActivity.this, GoodsDetailActivity.class, bundle);
            }
        });

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                int status = list.get(position).status;
                if (status == 1) {
                    ToastUtil.showToast(OrderDetailActivity.this, "等待卖家发货");
                } else if (status == 2) {
                    ContentValues values = new ContentValues();
                    values.put("status", 3);
                    DataSupport.updateAll(GoodsBean.class, values, "goods_id=?", list.get(position).goods_id);
                    list.get(position).status = 3;
                    adapter.notifyDataSetChanged();
                    ToastUtil.showToast(OrderDetailActivity.this, "交易成功");
                    EventBus.getDefault().post(new EventMessage(EventMessage.REFRESH));
                } else if (status == 3) {

                    bundle.putSerializable("bean", list.get(position));
                    showActivity(OrderDetailActivity.this, TalkActivity.class, bundle);
                }
            }
        });
    }



}
